package team.first.movieapiv2pro.controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import team.first.movieapiv2pro.domain.Comment;
import team.first.movieapiv2pro.service.CommentService;
import team.first.movieapiv2pro.vo.ApiResponse;
import team.first.movieapiv2pro.vo.ResponseComment;

import java.util.List;

/**
 * @Author: HAPPY
 * @Project_name: project
 * @Package_name: team.first.movieapiv2.controller
 * @Date: 2021/12/9 13:19
 * @Description:
 */
@RestController
public class CommentController {
    @Autowired
    private CommentService commentService;

    @GetMapping("/api/v2/movie/{id}/comments")
    @ApiOperation("1.6 GET： 获取指定编号的电影评论")
    public ApiResponse<List<ResponseComment>> getMovieComment(@PathVariable("id") Integer movieId) {
        ApiResponse<List<ResponseComment>> apiResponse = new ApiResponse<>();
        List<ResponseComment> responseCommentList = commentService.findMovieComment(movieId);
        apiResponse.setCode(200).setSuccess(true).setMessage("请求成功").setData(responseCommentList);
        return apiResponse;
    }

    /**
     * 2.3 POST 用户对指定电影进行评论
     *
     * @param comment 评论信息
     * @return 返回评论后的信息
     */
    @ApiOperation(value = "2.3 POST 对电影进行评论，返回评论后的电影信息")
    @PostMapping("/api/v2/comment")
    public ApiResponse<Integer> comment(Comment comment) {
        ApiResponse<Integer> apiResponse = new ApiResponse<>();

        // 将评论的电影编号作为响应数据返回
        if (commentService.saveComment(comment)) {
            apiResponse.setCode(200).setSuccess(true).setMessage("评论成功").setData(comment.getMovieId());
        } else {
            apiResponse.setCode(500).setSuccess(false).setMessage("评论失败").setData(comment.getMovieId());
        }

        return apiResponse;
    }

}
